<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-cmath.html" />
<link rel="prev" href="numeric.html" />
<link rel="parent" href="numeric.html" />
<link rel="next" href="module-cmath.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>6.1 math -- Mathematical functions</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="6.2 cmath  "
  href="module-cmath.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-cmath.html">6.2 cmath  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION008100000000000000000">
6.1 <tt class="module">math</tt> --
         Mathematical functions</a>
</h1>

<p>
<a name="module-math"></a>

<p>
This module is always available.  It provides access to the
mathematical functions defined by the C standard.

<p>
These functions cannot be used with complex numbers; use the functions
of the same name from the <tt class="module"><a href="module-cmath.html">cmath</a></tt> module if you require
support for complex numbers.  The distinction between functions which
support complex numbers and those which don't is made since most users
do not want to learn quite as much mathematics as required to
understand complex numbers.  Receiving an exception instead of a
complex result allows earlier detection of the unexpected complex
number used as a parameter, so that the programmer can determine how
and why it was generated in the first place.

<p>
The following functions are provided by this module.  Except
when explicitly noted otherwise, all return values are floats.

<p>
Number-theoretic and representation functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-931' xml:id='l2h-931' class="function">ceil</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the ceiling of <var>x</var> as a float, the smallest integer value
greater than or equal to <var>x</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-932' xml:id='l2h-932' class="function">fabs</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the absolute value of <var>x</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-933' xml:id='l2h-933' class="function">floor</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the floor of <var>x</var> as a float, the largest integer value
less than or equal to <var>x</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-934' xml:id='l2h-934' class="function">fmod</tt></b>(</nobr></td>
  <td><var>x, y</var>)</td></tr></table></dt>
<dd>
Return <code>fmod(<var>x</var>, <var>y</var>)</code>, as defined by the platform C library.
Note that the Python expression <code><var>x</var> % <var>y</var></code> may not return
the same result.  The intent of the C standard is that
<code>fmod(<var>x</var>, <var>y</var>)</code> be exactly (mathematically; to infinite
precision) equal to <code><var>x</var> - <var>n</var>*<var>y</var></code> for some integer
<var>n</var> such that the result has the same sign as <var>x</var> and
magnitude less than <code>abs(<var>y</var>)</code>.  Python's
<code><var>x</var> % <var>y</var></code> returns a result with the sign of
<var>y</var> instead, and may not be exactly computable for float arguments.
For example, <code>fmod(-1e-100, 1e100)</code> is <code>-1e-100</code>, but the
result of Python's <code>-1e-100 % 1e100</code> is <code>1e100-1e-100</code>, which
cannot be represented exactly as a float, and rounds to the surprising
<code>1e100</code>.  For this reason, function <tt class="function">fmod()</tt> is generally
preferred when working with floats, while Python's
<code><var>x</var> % <var>y</var></code> is preferred when working with integers.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-935' xml:id='l2h-935' class="function">frexp</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the mantissa and exponent of <var>x</var> as the pair
<code>(<var>m</var>, <var>e</var>)</code>.  <var>m</var> is a float and <var>e</var> is an
integer such that <code><var>x</var> == <var>m</var> * 2**<var>e</var></code> exactly.
If <var>x</var> is zero, returns <code>(0.0, 0)</code>, otherwise
<code>0.5 &lt;= abs(<var>m</var>) &lt; 1</code>.  This is used to "pick apart" the
internal representation of a float in a portable way.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-936' xml:id='l2h-936' class="function">ldexp</tt></b>(</nobr></td>
  <td><var>x, i</var>)</td></tr></table></dt>
<dd>
Return <code><var>x</var> * (2**<var>i</var>)</code>.  This is essentially the inverse of
function <tt class="function">frexp()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-937' xml:id='l2h-937' class="function">modf</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the fractional and integer parts of <var>x</var>.  Both results
carry the sign of <var>x</var>, and both are floats.
</dl>

<p>
Note that <tt class="function">frexp()</tt> and <tt class="function">modf()</tt> have a different
call/return pattern than their C equivalents: they take a single
argument and return a pair of values, rather than returning their
second return value through an `output parameter' (there is no such
thing in Python).

<p>
For the <tt class="function">ceil()</tt>, <tt class="function">floor()</tt>, and <tt class="function">modf()</tt>
functions, note that <em>all</em> floating-point numbers of sufficiently
large magnitude are exact integers.  Python floats typically carry no more
than 53 bits of precision (the same as the platform C double type), in
which case any float <var>x</var> with <code>abs(<var>x</var>) &gt;= 2**52</code>
necessarily has no fractional bits.

<p>
Power and logarithmic functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-938' xml:id='l2h-938' class="function">exp</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return <code>e**<var>x</var></code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-939' xml:id='l2h-939' class="function">log</tt></b>(</nobr></td>
  <td><var>x</var><big>[</big><var>, base</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return the logarithm of <var>x</var> to the given <var>base</var>.
If the <var>base</var> is not specified, return the natural logarithm of <var>x</var>
(that is, the logarithm to base <em>e</em>).

<span class="versionnote">Changed in version 2.3:
<var>base</var> argument added.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-940' xml:id='l2h-940' class="function">log10</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the base-10 logarithm of <var>x</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-941' xml:id='l2h-941' class="function">pow</tt></b>(</nobr></td>
  <td><var>x, y</var>)</td></tr></table></dt>
<dd>
Return <code><var>x</var>**<var>y</var></code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-942' xml:id='l2h-942' class="function">sqrt</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the square root of <var>x</var>.
</dl>

<p>
Trigonometric functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-943' xml:id='l2h-943' class="function">acos</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the arc cosine of <var>x</var>, in radians.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-944' xml:id='l2h-944' class="function">asin</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the arc sine of <var>x</var>, in radians.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-945' xml:id='l2h-945' class="function">atan</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the arc tangent of <var>x</var>, in radians.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-946' xml:id='l2h-946' class="function">atan2</tt></b>(</nobr></td>
  <td><var>y, x</var>)</td></tr></table></dt>
<dd>
Return <code>atan(<var>y</var> / <var>x</var>)</code>, in radians.
The result is between <code>-pi</code> and <code>pi</code>.
The vector in the plane from the origin to point <code>(<var>x</var>, <var>y</var>)</code>
makes this angle with the positive X axis.
The point of <tt class="function">atan2()</tt> is that the signs of both inputs are
known to it, so it can compute the correct quadrant for the angle.
For example, <code>atan(1</code>) and <code>atan2(1, 1)</code> are both <code>pi/4</code>,
but <code>atan2(-1, -1)</code> is <code>-3*pi/4</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-947' xml:id='l2h-947' class="function">cos</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the cosine of <var>x</var> radians.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-948' xml:id='l2h-948' class="function">hypot</tt></b>(</nobr></td>
  <td><var>x, y</var>)</td></tr></table></dt>
<dd>
Return the Euclidean norm, <code>sqrt(<var>x</var>*<var>x</var> + <var>y</var>*<var>y</var>)</code>.
This is the length of the vector from the origin to point
<code>(<var>x</var>, <var>y</var>)</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-949' xml:id='l2h-949' class="function">sin</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the sine of <var>x</var> radians.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-950' xml:id='l2h-950' class="function">tan</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the tangent of <var>x</var> radians.
</dl>

<p>
Angular conversion:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-951' xml:id='l2h-951' class="function">degrees</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Converts angle <var>x</var> from radians to degrees.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-952' xml:id='l2h-952' class="function">radians</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Converts angle <var>x</var> from degrees to radians.
</dl>

<p>
Hyperbolic functions:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-953' xml:id='l2h-953' class="function">cosh</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the hyperbolic cosine of <var>x</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-954' xml:id='l2h-954' class="function">sinh</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the hyperbolic sine of <var>x</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-955' xml:id='l2h-955' class="function">tanh</tt></b>(</nobr></td>
  <td><var>x</var>)</td></tr></table></dt>
<dd>
Return the hyperbolic tangent of <var>x</var>.
</dl>

<p>
The module also defines two mathematical constants:

<p>
<dl><dt><b><tt id='l2h-956' xml:id='l2h-956'>pi</tt></b></dt>
<dd>
The mathematical constant <em>pi</em>.
</dd></dl>

<p>
<dl><dt><b><tt id='l2h-957' xml:id='l2h-957'>e</tt></b></dt>
<dd>
The mathematical constant <em>e</em>.
</dd></dl>

<p>
<div class="note"><b class="label">Note:</b>
The <tt class="module">math</tt> module consists mostly of thin wrappers around
  the platform C math library functions.  Behavior in exceptional cases is
  loosely specified by the C standards, and Python inherits much of its
  math-function error-reporting behavior from the platform C
  implementation.  As a result,
  the specific exceptions raised in error cases (and even whether some
  arguments are considered to be exceptional at all) are not defined in any
  useful cross-platform or cross-release way.  For example, whether
  <code>math.log(0)</code> returns <code>-Inf</code> or raises <tt class="exception">ValueError</tt> or
  <tt class="exception">OverflowError</tt> isn't defined, and in
  cases where <code>math.log(0)</code> raises <tt class="exception">OverflowError</tt>,
  <code>math.log(0L)</code> may raise <tt class="exception">ValueError</tt> instead.
</div>

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-cmath.html">cmath</a></tt>:</b>
    <dd>Complex number versions of many of these functions.
  </dl>
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="6. numeric and Mathematical"
  href="numeric.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="6.2 cmath  "
  href="module-cmath.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="numeric.html">6. Numeric and Mathematical</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-cmath.html">6.2 cmath  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
